package com.share.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.exception.ServiceException;
import com.share.common.security.utils.SecurityUtils;
import com.share.device.domain.Cabinet;
import com.share.device.domain.CabinetType;
import com.share.device.mapper.CabinetMapper;
import com.share.device.mapper.CabinetTypeMapper;
import com.share.device.service.ICabinetTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/10/10 上午10:06
 */
@Service
@RequiredArgsConstructor
public class CabinetTypeServiceImpl extends ServiceImpl<CabinetTypeMapper, CabinetType> implements ICabinetTypeService {

    private final CabinetTypeMapper cabinetTypeMapper;
    private final CabinetMapper cabinetMapper;

    /**
     * 查询柜机类型列表
     *
     * @param cabinetType
     * @return
     */
    @Override
    public List<CabinetType> selectCabinetTypeList(CabinetType cabinetType) {
        return cabinetTypeMapper.selectCabinetTypeList(cabinetType);
    }

    /**
     * 新增柜机类型
     *
     * @param cabinetType
     * @return
     */
    @Override
    public int saveCabinetType(CabinetType cabinetType) {
        //判断柜机类型名称是否已经存在
        long count = this.count(new LambdaQueryWrapper<CabinetType>().eq(CabinetType::getName, cabinetType.getName()));
        if (count > 0) {
            throw new ServiceException("该柜机类型名称已经存在");
        }
        cabinetType.setCreateBy(SecurityUtils.getUsername());
        cabinetType.setCreateTime(new Date());
        cabinetType.setUpdateTime(new Date());
        return cabinetTypeMapper.insert(cabinetType);
    }

    /**
     * 修改柜机类型
     *
     * @param cabinetType
     * @return
     */
    @Override
    public int updateCabinetType(CabinetType cabinetType) {
        //获取旧的柜机类型信息
        CabinetType oldCabinetType = cabinetTypeMapper.selectById(cabinetType.getId());
        //判断数据是否存在
        if (oldCabinetType == null) {
            throw new ServiceException("要修改的数据不存在");
        }
        //判断修改后的柜机类型名称是否存在
        if (!cabinetType.getName().equals(oldCabinetType.getName())) {
            long count = this.count(new LambdaQueryWrapper<CabinetType>().eq(CabinetType::getName, cabinetType.getName()));
            if (count > 0) {
                throw new ServiceException("该柜机类型名称已经存在");
            }
        }
        cabinetType.setUpdateBy(SecurityUtils.getUsername());
        cabinetType.setUpdateTime(new Date());
        return cabinetTypeMapper.updateById(cabinetType);
    }

    /**
     * 修改柜机状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public int updateStatus(Long id, String status) {
        //1.获取旧的柜机类型信息
        CabinetType cabinetType = cabinetTypeMapper.selectById(id);

        //2.判断数据是否存在
        if (ObjectUtils.isEmpty(cabinetType)) {
            throw new ServiceException("要修改的数据柜机类型不存在！");
        }

        //3.判断要修改的状态是否停用还是启用 0.正常 1.停用
        if (Objects.equals(status, "1")) {
            //如果是要停用该柜机类型的话，判断该柜机类型下是否有柜机关联
            //根据柜机类型id查询柜机列表
            Long count = cabinetMapper.selectCount(new LambdaQueryWrapper<Cabinet>().eq(Cabinet::getCabinetTypeId, id));
            if (count > 0) {
                throw new ServiceException("该柜机类型下有柜机关联，请先解除关联！");
            }
        }

        //4.修改柜机类型状态
        cabinetType.setStatus(status);
        return cabinetTypeMapper.updateById(cabinetType);
    }

    /**
     * 查询可用的柜机类型列表
     *
     * @return
     */
    @Override
    public List<CabinetType> getAvailableCabinetTypes() {
        LambdaQueryWrapper<CabinetType> lambdaQueryWrapper = new LambdaQueryWrapper<CabinetType>().eq(CabinetType::getStatus, "0");
        return cabinetTypeMapper.selectList(lambdaQueryWrapper);
    }
}
